﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Reflection;
using System.Linq;

namespace CommunitySystem.Ctrls
{
    public class DictComboBox : ComboBox
    {
        private string _DictCode;

        public string DictCode
        {
            get { return _DictCode; }
            set { _DictCode = value; }
        }

        bool _HasAll = false;
        public bool HasAll
        {
            get { return _HasAll; }
            set { _HasAll = value; }
        }

        /// <summary>
        /// 参数的书写格式为: param=value
        /// Sql中的参数书写格式为: column=:param
        /// </summary>
        public string[] Parameter
        {
            get { return _Parameter; }
            set
            {
                _Parameter = value;
                foreach (string s in _Parameter)
                {
                    _ParamString += s + ";";
                }
                this.OnApplyTemplate();
            }
        }

        private string _ParamString = string.Empty;

        private string[] _Parameter;


        private CommunitySystem.DataService.DataServiceClient client = null;
        public DictComboBox()
            : base()
        {
            this.Loaded += new RoutedEventHandler(DictComboBox_Loaded);
            client = new DataService.DataServiceClient();
           
           
            client.GetDictModelCompleted += new EventHandler<DataService.GetDictModelCompletedEventArgs>(client_GetDictModelCompleted);

        }

        void DictComboBox_Loaded(object sender, RoutedEventArgs e)
        {
            if (Field != null)
            {
                this.SetBinding(DictComboBox.SelectedValueProperty, new System.Windows.Data.Binding(Field)
                {
                    Mode = System.Windows.Data.BindingMode.TwoWay,
                    NotifyOnValidationError = true,
                    ValidatesOnExceptions = true,
                });
            }
            if (this.ItemsSource == null)
            {

                if (CommunitySystem.Common.InitialDictionary.Dict.Keys.Where(c => c.Equals(DictCode)).Count() <= 0)
                {
                    //client.GetDictModelAsync(DictCode, _ParamString);
                    CommunitySystem.Common.InitialDictionary init = new Common.InitialDictionary();
                    init.RefreshDictionary(DictCode, _ParamString, this);
                }
                else
                {

                    client_GetDictModelCompleted(null, new CommunitySystem.DataService.GetDictModelCompletedEventArgs(new object[1] { CommunitySystem.Common.InitialDictionary.Dict.Where(c => c.Key.Equals(DictCode)).FirstOrDefault().Value }, null, false, null));
                }
                //client.GetDictModelAsync(DictCode, _ParamString);
            }
        }

        public string Field { set; get; }
        public void client_GetDictModelCompleted(object sender, DataService.GetDictModelCompletedEventArgs e)
        {
            var result = e.Result;
            if (!result.IsTree)
            {
                var res = result.Item;
                if (_HasAll == true)
                {
                    res.Insert(0, new DataService.DataDictionaryItemModel()
                    {
                        DictCode = "全部",
                        Id = 0,
                        DictValue = "全部"
                    });
                }

                base.ItemsSource = res;

                //if (_HasAll != true)
                //{


                if (this.DataContext != null && !string.IsNullOrEmpty(Field))
                {
                    PropertyInfo pi = this.DataContext.GetType().GetProperty(Field);
                    if (pi == null)
                        return;
                    object value = pi.GetValue(this.DataContext, null);
                    if (value != null)
                    {
                        foreach (object obj in res)
                        {
                            object dictvalue = obj.GetType().GetProperty(this.SelectedValuePath).GetValue(obj, null);
                            if (dictvalue.Equals(value))
                            {
                                int index = res.IndexOf(obj as CommunitySystem.DataService.DataDictionaryItemModel);
                                this.SelectedIndex = index;
                                break;
                            }

                        }
                    }
                }
            }
            //}
        }
    }
}
